<?php
namespace App\Services;

use App\Api\Models\StorageManageModel;
use  App\Models\MaterielTypeModel;

class MaterielTypeService {
    static public function materielTypeList($parentId, $data)
    {
        $condition = [];

        if(!empty($data['materiel_class_id'])) {
            $condition['a.materiel_class_id'] = ['EQ' => $data['materiel_class_id']];
        }

        $keyword = empty($data['keyword'])?'':$data['keyword'];

        return $data = MaterielTypeModel::getMaterielTypeList($parentId, $condition, $keyword, $data);
    }

    //新建物料类型
    static public function add($parentId, $data)
    {
        $data['created_at'] = time();
        $data['main_id'] = $parentId;
        $data['remark'] = empty($data['remark'])?'':$data['remark'];
        $data['materiel_img'] = empty($data['materiel_img'])?'':$data['materiel_img'];

        $condition['materiel_no'] = $data['materiel_no'];
        $condition['main_id'] = $parentId;

        //验证物料类型信息
        $checkMaterielNo = MaterielTypeModel::checkMaterielTypeCode($data['materiel_no'], $parentId);
        if(!empty($checkMaterielNo)) {
            return ['code' => 1, 'msg' => '物料编号已存在'];
        }

        //验证物料类型信息
        $checkMaterielInfo = MaterielTypeModel::checkMaterielInfo($data);
        if(!empty($checkMaterielInfo)) {
            return ['code' => 1, 'msg' => '物料类型`'.$data['materiel_name'].'-'.$data['type_name'].'`已存在'];
        }

        $insertId = MaterielTypeModel::insertGetId($data);
        return ['code' => 0, 'data' => $insertId];
    }

    //修改物料信息
    static public function edit($parentId, $data)
    {
        $condition[] = ['id', '!=', $data['id']];
        $condition['materiel_no'] = $data['materiel_no'];
        $condition['main_id'] = $parentId;

        //分类编号已存在时，重新生成
        $checkMaterielNo = MaterielTypeModel::where([
                                            'materiel_no' => $data['materiel_no'],
                                            'main_id' => $parentId,
                                        ])
                                        ->where('id', '!=', $data['id'])
                                        ->first();
        if(!empty($checkMaterielNo)) {
            return ['code' => 1, 'msg' => '类型编号已存在'];
        }

        //校验类型和型号是否已存在
        $checkMaterielTypeInfo = MaterielTypeModel::where([
                                                        'main_id' => $parentId,
                                                        'materiel_name' => $data['materiel_name'],
                                                        'type_name' => $data['type_name'],
                                                        'delete_flag' => 0,
                                                    ])
                                                    ->where('id', '!=', $data['id'])
                                                    ->first();

        if(!empty($checkMaterielTypeInfo)) {
            return ['code' => 1, 'msg' => '物料类型及型号已存在'];
        }

        $result = MaterielTypeModel::where('id', $data['id'])->update($data);

        $storageManageInfo = StorageManageModel::where('type_id', $data['id'])
            ->get()->toArray();

        if(!empty($storageManageInfo)) {
            $storageManageInfo->materiel_class_id = $data['materiel_class_id'];
            $storageManageInfo->save();
        }

        if($result !== false) {
            return ['code' => 0];
        }
    }

    //物料类型详细信息
    static public function info($id)
    {
        $info = MaterielTypeModel::select(
                                    'a.id',
                                    'a.materiel_name',
                                    'a.type_name',
                                    'a.materiel_img',
                                    'a.materiel_no',
                                    'a.materiel_class_id',
                                    'a.remark',
                                    'a.length',
                                    'a.width',
                                    'a.height',
                                    'a.weight',
                                    'a.attribute',
                                    'a.cost',
                                    'b.class_name as sec_class_name',
                                    'c.id as fir_class_id',
                                    'c.class_name as fir_class_name',
                                    'a.is_album'
                                )
                                ->from('materiel_type as a')
                                ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
                                ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
                                ->where('a.id', $id)
                                ->where('a.delete_flag', 0)
                                ->first();

        return $info;
    }

    static public function materielTypeSelect($mainId, $keyword, $condition, $pageSize, $currentPage)
    {
        return MaterielTypeModel::select(
            'a.id',
            'materiel_name',
            'type_name',
            'materiel_img',
            'materiel_no',
            'materiel_class_id',
            'b.class_name as sec_class_name',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.is_use_serial'
        )
        ->from('materiel_type as a')
        ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
        ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
        ->where($condition)
        ->where(function($query) use ($keyword) {
            $query->orWhere('a.materiel_name', 'like', '%'.$keyword.'%');
            $query->orWhere('a.type_name', 'like', '%'.$keyword.'%');
            $query->orWhere('a.materiel_no', 'like', '%'.$keyword.'%');
            $query->orWhere('b.class_name', 'like', '%'.$keyword.'%');
            $query->orWhere('c.class_name', 'like', '%'.$keyword.'%');
        })
        ->where('a.delete_flag', 0)
        ->where('a.main_id', $mainId)
        ->orderBy('a.id', 'DESC')
        ->paginate($pageSize,['*'], '',$currentPage);
    }
}
